package com.doyoung.appicker.controller;

import java.util.ArrayList;

import java.util.HashMap;

import com.doyoung.appicker.R;
import com.doyoung.appicker.util.ImageLoader;
import com.doyoung.appicker.view.MainListFragment;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SongController extends BaseAdapter{
	
	private Activity activity;
	private ArrayList<HashMap<String, String>> data;
	private static LayoutInflater inflater = null;
	public ImageLoader imageLoader;
	
	public SongController(Activity a, ArrayList<HashMap<String, String>> d){
		activity = a;
		data = d;
		inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(activity.getApplicationContext());
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
        if(convertView==null)
            v = inflater.inflate(R.layout.list_row, null);
 
        TextView title = (TextView)v.findViewById(R.id.title); // title
        TextView artist = (TextView)v.findViewById(R.id.artist); // artist name
        TextView duration = (TextView)v.findViewById(R.id.duration); // duration
        ImageView thumb_image=(ImageView)v.findViewById(R.id.list_image); // thumb image
 
        HashMap<String, String> app = new HashMap<String, String>();
        app = data.get(position);
 
        // Setting all values in listview
        title.setText(app.get(MainListFragment.KEY_TITLE));
        artist.setText(app.get(MainListFragment.KEY_ARTIST));
        duration.setText(app.get(MainListFragment.KEY_DURATION));
        imageLoader.DisplayImage(app.get(MainListFragment.KEY_THUMB_URL), thumb_image);
        
        return v;
	}

	
}
